<!doctype html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
	<meta name="format-detection" content="telephone=no" />
	<title>首页</title>
	<link rel="stylesheet" type="text/css" href="../c/aui.css" />
</head>
<style>
	.aui-content {
		color: #fff;
	}

	.sx-info .aui-card-list-footer {
		background: url('../i/right_s.png') no-repeat right center;
		background-size: 0.3rem;
		margin-right: 0.75rem;
		padding-right: 0.8rem;
		color: #fff;
		font-size: 0.75rem;
		opacity: 0.8;
		margin-bottom: 0.75rem;
	}

	.sx-info .aui-card-list-footer div:first-child {
		width: 20%;
	}

	.sx-info .aui-card-list-footer div:last-child {
		opacity: 0.6;
		width: 75%;
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		word-wrap: break-word;
		word-break: break-all;
		white-space: normal !important;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
		text-align: right;
	}

	.btn {
		color: #fff;
		text-align: center;
		padding: 0.75rem;
		margin: 0.75rem;
		font-size: 0.7rem;
		opacity: 0.5;
		padding-top: 0rem;
	}

	#banben {
		font-size: 0.65rem;
		color: #fff;
		text-align: center;
		opacity: 0.3;
		padding-top: 1rem;
	}

	#banben span {
		font-weight: bold;
	}
</style>
<body>
	<div class="aui-content">
		<div class="sx-info">
			<div class="aui-card-list-footer" tapmode onclick="_loginTyle('wx')">
				<div>微信</div>
				<div id="wxapp">未绑定</div>
			</div>
			<div class="aui-card-list-footer" tapmode onclick="_loginTyle('qq')">
				<div>QQ</div>
				<div id="qqapp">未绑定</div>
			</div>
			<div class="aui-card-list-footer" tapmode onclick="_loginTyle('wb')">
				<div>微博</div>
				<div id="wbapp">未绑定</div>
			</div>
		</div>
	</div>
</body>
<script type="text/javascript" src="../j/api.js"></script>
<script type="text/javascript">
	var _root = 0;
	apiready = function() {
		api.parseTapmode();
		a();
	}
	function a(){
		_loading();
		_ajax('member/band_info',function(ret,err){
			if(ret){
				if(ret.length){
					for (var i = 0; i < ret.length; i++) {
						$('#'+ret[i].type).text('已绑定');
					}
				}
			}
			_loadingCloes();
			_root = 1;
		})
	}
	// 第三方登陆
	function _loginTyle(type) {
		if(!_root){
			return _msg('等待数据加载');
		}
		var text = $('#'+type+'app').text();
		if(text == '已绑定'){
			return _msg('该平台已绑定，不支持更换');
		}
		document.activeElement.blur();
		if (type == 'wx') {
			var wx = api.require('wxPlus');
			wx.isInstalled(function(ret, err) {
				if (ret.installed) {
					wx.auth({}, function(reta, erra) {
						if (reta.status) {
							wx.getToken({
								code: reta.code
							}, function(retg, errg) {
								if (retg.status) {
									wx.getUserInfo({
										accessToken: retg.accessToken,
										openId: retg.openId
									}, function(retw, errw) {
										if (retw.status) {
											if (retw.openid) {
												_loginToken({
													type: 'wxapp',
													openid: retw.openid,
													unionid: retw.unionid,
													img: retw.headimgurl,
													nickname: retw.nickname,
													info: retw
												})
											} else {
												_loadingCloes();
												_alert('openId值为空')
											}
										} else {
											_loadingCloes();
										}
									});
								} else {
									_loadingCloes();
								}
							});
						} else {
							_loadingCloes();
						}
					});
				} else {
					_loadingCloes();
					_alert('当前设备未安装微信客户端')
				}
			});
		} else if (type == 'qq') {
			var qq = api.require('QQPlus');
			qq.installed(function(ret, err) {
				if (ret.status) {
					qq.login(function(retl, errl) {
						if (retl.status) {
							//	console.log(JSON.stringify(retl))
							qq.getUserInfo(function(retg, errg) {
								var openid = retl.openId;
								if (openid && retg.status) {

									if (api.systemType == 'ios') {

									} else {
										retg.info = eval('(' + retg.info.replace(/}{/, ',') + ')');
									}
									console.log(retg.info);
									_loginToken({
										type: 'qqapp',
										openid: openid,
										unionid: retg.unionid,
										img: retg.info.figureurl_qq_2,
										nickname: retg.info.nickname,
										info: retg.info
									})
								} else {
									_loadingCloes();
									_alert('获取QQ资料失败')
								}
							});
						} else {
							_loadingCloes();
						}
					});
				} else {
					_loadingCloes();
					_alert('当前设备未安装QQ客户端')
				}
			});
		} else if (type == 'wb') {
			var weibo = api.require('weiboPlus');
			weibo.isInstalled(function(ret) {
				_loadingCloes();
				if (ret.status) {
					weibo.auth(function(reta, erra) {
						if (reta) {
							if (reta.status) {
								weibo.getUserInfo({
									token: reta.token,
									userId: reta.userId
								}, function(retg, errg) {
									if (retg.status) {
										var openid = retg.userInfo.id
										if (openid) {
											_loginToken({
													type: 'wbapp',
													openid: openid,
													img: retg.userInfo.avatar_hd,
													nickname: retg.userInfo.name,
													info: retg.userInfo
												})
												//	_login(openid, 'wbapp')
										} else {
											_loadingCloes();
											_alert('openId值为空')
										}
										return;
									} else {
										_loadingCloes();
									}
								});
							} else {
								_loadingCloes();
							}
						} else {
							_loadingCloes();
						}
					});
				} else {
					api.alert({
						msg: '当前设备未安装新浪微博客户端'
					});
					return
				}
			});
		}
	}

	// 封装ajax登陆获取token
	function _loginToken(data) {
		_loading();
		_ajax('member/oauth', function(ret, err) {
			_loadingCloes()
			a();
			if (ret) {
				_root = 0;
				if(ret.ret){
					_msg('绑定成功');
				}else{
					_msg(ret.err);
				}
				// _alert(JSON.stringify(ret))
			} else {
				console.log(JSON.stringify(err))
				_alert(err)
			}
		}, data)
	}

</script>

</html>
